package com.sky.controller.admin;

import com.sky.config.Calibration;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealWithDishService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@Api(tags = "套餐管理接口")
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealWithDishService setmealService;

    @PostMapping
    @ApiOperation("新增套餐")
    public Result saveSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐：{}", setmealDTO);
        //1.校验前端传递进来的参数
        Calibration.addedSetmealVerification(setmealDTO);
        //将数据传递到服务层处理业务
        setmealService.saveSetmeal(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1,校验参数
        log.info("套餐分页查询，参数：{}", setmealPageQueryDTO);
        Calibration.pageQuery(setmealPageQueryDTO);
        //2,调用服务层
        PageResult pageResult =setmealService.pageQuery(setmealPageQueryDTO);
        //3,返回结果
        return Result.success(pageResult);
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐：{}", setmealDTO);
        //1.校验前端传递进来的参数
        Calibration.addedSetmealVerification(setmealDTO);
        //2.将数据传递到服务层处理业务
        setmealService.updateWithFavorite(setmealDTO);
        //3.返回结果
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询套餐：{}", id);
        //1.校验参数
        if (Objects.isNull(id)){
            return Result.error("参数异常为空");
        }
        //2.调用服务层
        SetmealVO setmealVO = setmealService.getById(id);
        //3.返回结果
        return Result.success(setmealVO);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("起售停售套餐")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("起售停售套餐：{}", status);
        //1.校验参数
        if (Objects.isNull(id)){
            return Result.error("参数异常为空");
        }
        //2.调用服务层
        setmealService.startOrStop(status,id);
        //3.返回结果
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("批量删除套餐")
    public Result deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除套餐：{}", ids);
        //1.校验参数
        if (Objects.isNull(ids)){
            return Result.error("参数异常为空");
        }
        //2.调用服务层
        setmealService.deleteByIds(ids);
        //3.返回结果
        return Result.success();
    }
}
